class NewControlLink extends frappe.ui.form.ControlLink {
	make_input() {
		var me = this;
		$(`<div class="link-field ui-front" style="position: relative;">
			<input type="text" class="input-with-feedback form-control">
			<span class="link-btn">
				<a class="btn-open no-decoration" title="${__("Open Link")}">
					${frappe.utils.icon("arrow-right", "xs")}
				</a>
			</span>
		</div>`).prependTo(this.input_area);
		this.$input_area = $(this.input_area);
		this.$input = this.$input_area.find("input");
		this.$link = this.$input_area.find(".link-btn");
		this.$link_open = this.$link.find(".btn-open");
		this.set_input_attributes();
		this.$input.on("focus", function () {
			// 检查是否在弹窗中的子表
			let isInDialog = $(me.awesomplete.input).closest('.modal-dialog').length > 0;
			let isInGrid = $(me.awesomplete.input).closest('.form-grid').length > 0;
			let isInDialogGrid = isInDialog && isInGrid;
			
			// 处理弹窗中的子表 Link 字段
			if (isInDialogGrid) {
				console.log("处理弹窗中的子表 Link 字段");
				
				// 创建临时容器（与普通子表处理方式类似）
				var $temp_div_id = "dialog_grid_" + me.awesomplete.ul.id;
				var $temp_div = $("#" + $temp_div_id);
				if ($temp_div.length === 0) {
					$temp_div = $("<div class='awesomplete' id='" + $temp_div_id + "'></div>");
					$('body').append($temp_div);
				}
				
				// 获取原始下拉列表
				var $ul = $('#' + me.awesomplete.ul.id);
				$ul.appendTo($temp_div);
				
				// 获取输入框的位置
				var $xy = $(me.awesomplete.input).offset();
				
				// 获取弹窗的滚动位置
				let $modalBody = $(me.awesomplete.input).closest('.modal-body');
				let scrollTop = $modalBody.scrollTop() || 0;
				
				// 调整位置，考虑弹窗的滚动
				var $x = $xy.top - scrollTop + 30; // 调整垂直位置
				var $y = $xy.left;
				
				// 设置临时容器样式
				$temp_div.css({
					'position': 'fixed',
					'z-index': '9999',
					'top': $x + 'px',
					'left': $y + 'px'
				});
				
				// 设置下拉列表样式
				$ul.css({
					'display': 'block',
					'visibility': 'visible',
					'background-color': 'white',
					'border': '1px solid #d1d8dd',
					'border-radius': '3px',
					'box-shadow': '0px 3px 6px rgba(0, 0, 0, 0.15)',
					'min-width': $(me.awesomplete.input).outerWidth() + 'px',
					'max-height': '200px',
					'overflow-y': 'auto'
				});
				
				// 监听弹窗滚动事件，调整下拉列表位置
				$modalBody.on('scroll.awesomplete', function() {
					let newScrollTop = $modalBody.scrollTop() || 0;
					let newTop = $xy.top - newScrollTop + 30;
					$temp_div.css('top', newTop + 'px');
				});
				
				// 当输入框失去焦点时，移除滚动事件监听
				me.$input.one('blur', function() {
					$modalBody.off('scroll.awesomplete');
				});
			}
			// 处理普通子表的 Link 字段
			else if (isInGrid && !isInDialog) {
				// 创建临时容器
				var $temp_div_id = "grid_" + me.awesomplete.ul.id;
				var $temp_div = $("#" + $temp_div_id);
				if ($temp_div.length === 0) {
					$temp_div = $("<div class='awesomplete' id='" + $temp_div_id + "'></div>");
					$('body').append($temp_div);
				}
				
				// 获取原始下拉列表
				var $ul = $('#' + me.awesomplete.ul.id);
				$ul.appendTo($temp_div);
				
				// 获取输入框的位置
				var $xy = $(me.awesomplete.input).offset();
				var $x = $xy.top + 30; // 调整垂直位置
				var $y = $xy.left;
				
				// 设置临时容器样式
				$temp_div.css({
					'position': 'absolute',
					'z-index': '9999',
					'top': $x + 'px',
					'left': $y + 'px'
				});
				
				// 设置下拉列表样式
				$ul.css({
					'display': 'block',
					'visibility': 'visible',
					'background-color': 'white',
					'border': '1px solid #d1d8dd',
					'border-radius': '3px',
					'box-shadow': '0px 3px 6px rgba(0, 0, 0, 0.15)',
					'min-width': $(me.awesomplete.input).outerWidth() + 'px',
					'max-height': '200px',
					'overflow-y': 'auto'
				});
			}
			
			setTimeout(function () {
				if (me.$input.val() && me.get_options()) {
					let doctype = me.get_options();
					let name = me.get_input_value();
					me.$link.toggle(true);
					me.$link_open.attr("href", frappe.utils.get_form_link(doctype, name));
				}

				if (!me.$input.val()) {
					me.$input.val("").trigger("input");

					// hide link arrow to doctype if none is set
					me.$link.toggle(false);
				}
			}, 500);
		});
		this.$input.on("blur", function () {
			// if this disappears immediately, the user's click
			// does not register, hence timeout
			setTimeout(function () {
				me.$link.toggle(false);
			}, 500);
		});
		this.$input.attr("data-target", this.df.options);
		this.input = this.$input.get(0);
		this.has_input = true;
		this.translate_values = true;
		this.setup_buttons();
		this.setup_awesomeplete();
		this.bind_change_event();
	}
}

frappe.ui.form.ControlLink = NewControlLink;

// 重新定义 ControlDynamicLink 以使用新的 ControlLink 实现
class NewControlDynamicLink extends NewControlLink {
	get_options() {
		let options = "";
		if (this.df.get_options) {
			options = this.df.get_options(this);
		} else if (this.docname == null && cur_dialog) {
			//for dialog box
			options = cur_dialog.get_value(this.df.options);
		} else if (!cur_frm) {
			const selector = `input[data-fieldname="${this.df.options}"]`;
			let input = null;
			if (cur_list) {
				// for list page
				input = cur_list.filter_area.standard_filters_wrapper.find(selector);
			}
			if (cur_page) {
				input = $(cur_page.page).find(selector);
			}
			if (input) {
				options = input.val();
			}
		} else {
			options = frappe.model.get_value(this.df.parent, this.docname, this.df.options);
		}

		if (frappe.model.is_single(options)) {
			frappe.throw(__("{0} is not a valid DocType for Dynamic Link", [options.bold()]));
		}

		return options;
	}
}

frappe.ui.form.ControlDynamicLink = NewControlDynamicLink;